#include "stdafx.h"
#include "WorkThread.h"
#include <process.h>
#define  MAX_LENGHT 1024
#include "ConMgr.h"

unsigned __stdcall WorkThreadEnter( void* Param )
{
    WorkThread* pWorkThread = (WorkThread*)Param;
    if(pWorkThread)
    {
        pWorkThread->ThreadProc();
    }

    return 0;
}

WorkThread::WorkThread() : m_hThread(NULL), 
                       m_hUserSocket(NULL), 
                       m_hDevelopSocket(NULL),
                       m_dwMatchID(0),
                       m_uThreadID(0),
                       m_dwStart(0)
{
    Init();
}

WorkThread::~WorkThread()
{
    UnInit();
}

BOOL WorkThread::Init()
{
    if(m_hThread)
    {
        ASSERT(FALSE);
        return FALSE;
    }

    FD_ZERO(&m_frd);
    m_hThread = (HANDLE)_beginthreadex(NULL, 0, WorkThreadEnter, (void*)this, 0, &m_uThreadID);
    SetThreadPriority(m_hThread, THREAD_PRIORITY_NORMAL);

    m_dwStart = ::GetTickCount();

    return TRUE;
}

void WorkThread::UnInit()
{
    if(m_hThread != NULL)
    {
       FD_ZERO(&m_frd);
       CloseHandle(m_hThread);
    }
}

void WorkThread::SetUserSocket( SOCKET hSocket )
{
    m_hUserSocket = hSocket;
    FD_SET(m_hUserSocket, &m_frd);
}

SOCKET WorkThread::GetUserSocket()
{
    return m_hUserSocket;
}

void WorkThread::SetDevelopSocket( SOCKET hSocket )
{
    m_hDevelopSocket = hSocket;
    FD_SET(m_hDevelopSocket, &m_frd);
}

SOCKET WorkThread::GetDevelopSocket()
{
    return m_hDevelopSocket;
}

void WorkThread::SetMatchID( DWORD dwMatchID )
{
    m_dwMatchID = dwMatchID;
}

DWORD WorkThread::GetMatchID()
{
    return m_dwMatchID;
}

DWORD WINAPI WorkThread::ThreadProc( LPVOID lpParameter /*= NULL*/ )
{
    while(1)
    {
        if(m_hDevelopSocket == NULL || m_hUserSocket == NULL)
        {
            continue;
        }

        if(::GetTickCount() - m_dwStart > 10 * 60 * 1000)
        {
            ClearInfo();
        }
        if(select(0, &m_frd, NULL, NULL, 0) != SOCKET_ERROR)
        {
            if(FD_ISSET(m_hDevelopSocket, &m_frd))
            {
                m_dwStart = ::GetTickCount();
                char pBuf[MAX_LENGHT] = {0};
                int nRevSize = 0;
                if((nRevSize = recv(m_hDevelopSocket, pBuf, MAX_LENGHT, 0)) != SOCKET_ERROR)
                {
                    if(send(m_hUserSocket, pBuf, nRevSize, 0) == SOCKET_ERROR)
                    {
                        ASSERT(FALSE);
                    }
                }
            }

            if(FD_ISSET(m_hUserSocket, &m_frd))
            {
                m_dwStart = GetTickCount();
                char pBuf[MAX_LENGHT] = {0};
                int nRevSize = 0;
                if((nRevSize = recv(m_hUserSocket, pBuf, MAX_LENGHT, 0)) != SOCKET_ERROR)
                {
                    if(send(m_hDevelopSocket, pBuf, nRevSize, 0) == SOCKET_ERROR)
                    {
                        ASSERT(FALSE);
                    }
                }
            }
        }
    }
    return 0;
}

void WorkThread::CloseUserSocket()
{
    closesocket(m_hUserSocket);
    m_hUserSocket = NULL;
}

void WorkThread::CloseDevelopSocket()
{
    closesocket(m_hDevelopSocket);
    m_hDevelopSocket = NULL;
}

void WorkThread::ClearInfo()
{
    CloseUserSocket();
    CloseDevelopSocket();
    m_dwMatchID = 0;
    FD_ZERO(&m_frd);
    CConMrg::CloseConnect(this);
}

